import time

from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from flask_cors import CORS
from flask import Flask, request, json

app = Flask('google-routing-service')
CORS(app)


def createChromeDriver():
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument('--no-sandbox')
    chrome_options.add_argument('--disable-dev-shm-usage')
    
    # 将--user-data-dir改为自定义目录
    # 在linux服务器上执行，需要增加
    # chrome_options.add_argument("--user-data-dir=/root/googlemap")
    # s = Service(executable_path='/usr/local/bin/chromedriver')

    s = Service(executable_path='/Users/gilbert/chromedriver-mac-x64/chromedriver')
    _chrome_driver_instance = webdriver.Chrome(service=s, options=chrome_options)

    _chrome_driver_instance.maximize_window()

    return _chrome_driver_instance


def scrapyBaiduRoute(source, middle, destination):
    chrome_driver_instance = createChromeDriver()

    try:
        chrome_driver_instance.get("https://www.google.com/maps")

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH, "//button[@id='hArJGc']").click()

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH,
                                            "//div[@id='sb_ifc50']"
                                            "//input[contains(@class, 'tactile-searchbox-input')]").clear()

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH,
                                            "//div[@id='sb_ifc50']"
                                            "//input[contains(@class, 'tactile-searchbox-input')]").send_keys(source)

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH,
                                            "//div[@id='sb_ifc51']"
                                            "//input[contains(@class, 'tactile-searchbox-input')]").\
            send_keys(middle)

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH, "//div[@id='sb_ifc51']"
                                            "//input[contains(@class, 'tactile-searchbox-input')]").\
            send_keys(Keys.ENTER)

        time.sleep(5)

        chrome_driver_instance.find_element(By.XPATH, "//button[contains(@class, 'fC7rrc')]").click()

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH,
                                            "//div[@id='sb_ifc52']"
                                            "//input[contains(@class, 'tactile-searchbox-input')]"). \
            send_keys(destination)

        time.sleep(2)

        chrome_driver_instance.find_element(By.XPATH, "//div[@id='sb_ifc52']"
                                                      "//input[contains(@class, 'tactile-searchbox-input')]"). \
            send_keys(Keys.ENTER)

        time.sleep(5)

        chrome_driver_instance.find_element(By.XPATH, "//button[@data-tooltip='驾车']").click()

        time.sleep(3)

        return chrome_driver_instance.current_url

    except Exception as e:
        print(e.__str__())

        return ''
    finally:
        chrome_driver_instance.quit()


@app.route('/getRoutes', methods=['POST'])
def getRoutes():
    data = json.loads(json.dumps(request.get_json()))

    return scrapyBaiduRoute(data['start'], data['middle'], data['end'])


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=10010, debug=False)
